package com.kth.subtranslator.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class SaveSubDialog extends JDialog {
	
	public static interface ISaveListener {
		public void onSave(String absoluteFilePath);
	}

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JLabel lblSelectedDir;
	private File selectedDir;
	private JButton okButton;
	
	private List<ISaveListener> saveListeners = new ArrayList<ISaveListener>();
	private JLabel lblFileName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SaveSubDialog dialog = new SaveSubDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Create the dialog.
	 */
	public SaveSubDialog() {
		setTitle("Save Subtitle");
		setBounds(100, 100, 282, 149);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{44, 0, 71, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 47, 23, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lblDirectory = new JLabel("Directory");
			GridBagConstraints gbc_lblDirectory = new GridBagConstraints();
			gbc_lblDirectory.anchor = GridBagConstraints.EAST;
			gbc_lblDirectory.insets = new Insets(0, 0, 5, 20);
			gbc_lblDirectory.gridx = 0;
			gbc_lblDirectory.gridy = 0;
			contentPanel.add(lblDirectory, gbc_lblDirectory);
		}
		{
			JButton btnChoose = new JButton("Choose");
			btnChoose.addActionListener( new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					final JFileChooser fc = new JFileChooser();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					int returnVal = fc.showOpenDialog(SaveSubDialog.this);
					if (returnVal == 0) {
						File f = fc.getSelectedFile();
						if (f != null) {
							setCurrentSelectedDir( f );
							syncObButtonState();
							getLblSelectedDir().setText( f.getName() );
						}
					}
					
				}

			});
			{
				lblSelectedDir = new JLabel("");
				GridBagConstraints gbc_lblSelectedDir = new GridBagConstraints();
				gbc_lblSelectedDir.insets = new Insets(0, 0, 5, 5);
				gbc_lblSelectedDir.gridx = 1;
				gbc_lblSelectedDir.gridy = 0;
				contentPanel.add(lblSelectedDir, gbc_lblSelectedDir);
			}
			GridBagConstraints gbc_btnChoose = new GridBagConstraints();
			gbc_btnChoose.insets = new Insets(0, 0, 5, 0);
			gbc_btnChoose.gridx = 2;
			gbc_btnChoose.gridy = 0;
			contentPanel.add(btnChoose, gbc_btnChoose);
		}
		{
			this.lblFileName = new JLabel("File Name");
			GridBagConstraints gbc_lblFileName = new GridBagConstraints();
			gbc_lblFileName.anchor = GridBagConstraints.EAST;
			gbc_lblFileName.insets = new Insets(0, 0, 0, 20);
			gbc_lblFileName.gridx = 0;
			gbc_lblFileName.gridy = 1;
			contentPanel.add(lblFileName, gbc_lblFileName);
		}
		{
			textField = new JTextField();
			textField.getDocument().addDocumentListener( new DocumentListener() {
				
				@Override
				public void removeUpdate(DocumentEvent e) {
					syncObButtonState();
				}
				
				@Override
				public void insertUpdate(DocumentEvent e) {
					syncObButtonState();
				}
				
				@Override
				public void changedUpdate(DocumentEvent e) {
					syncObButtonState();
				}
			});
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.gridwidth = 2;
			gbc_textField.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField.gridx = 1;
			gbc_textField.gridy = 1;
			contentPanel.add(textField, gbc_textField);
			textField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				this.okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.setEnabled(false);
				okButton.addActionListener( new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						String fileSeparator = System.getProperty("file.separator");
						fireSaveListener( getCurrentSelectedDir().getAbsolutePath() + fileSeparator + getTextField().getText() );
						setVisible(false);
					}
				} );
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener( new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
			}
		}
	}
	
	
	public JLabel getLblSelectedDir() {
		return lblSelectedDir;
	}
	
	public void setCurrentSelectedDir(File f) {
		this.selectedDir = f;
	}
	
	public File getCurrentSelectedDir() {
		return selectedDir;
	}
	
	public JButton getOkButton() {
		return okButton;
	}
	
	public JLabel getLblFileName() {
		return lblFileName;
	}
	
	public JTextField getTextField() {
		return textField;
	}
	
	public void addSaveListener(ISaveListener l) {
		saveListeners.add(l);
	}
	
	private void fireSaveListener(String absoluteFilePath) {
		for (ISaveListener l : saveListeners) {
			l.onSave(absoluteFilePath);
		}
	}
	
	private void syncObButtonState() {
		if (getCurrentSelectedDir() != null && textField != null && !textField.getText().isEmpty()) {
			getOkButton().setEnabled(true);
		}
		else {
			getOkButton().setEnabled(false);
		}
	}
}
